<template>
    <div class="ve_container">
        <!-- 搜索 -->
        <el-form ref="queryForm" :inline="true" :model="params">
            <el-form-item label="名称" prop="name">
                <el-input
                    clearable
                    v-model="name"
                    placeholder="请输入"
                ></el-input>
            </el-form-item>
            <el-form-item>
                <el-button
                    type="primary"
                    @click="onSubmit(params, getDataList)"
                >
                    {{ buttons.search.name }}
                </el-button>
                <el-button @click="resetForm(queryForm, params, getDataList)">
                    重置
                </el-button>
            </el-form-item>
        </el-form>

        <!-- 列表 -->
        <ve-table
            :table="{
                data: tableData,
            }"
            :pagination="{
                onSizeChange: (val) =>
                    handleSizeChange(val, params, getDataList),
                onCurrentChange: (val) =>
                    handleCurrentChange(val, params, getDataList),
                currentPage: current,
                pageSize: size,
                total: total,
            }"
        >
            <template #tool_bar>
                <el-button
                    v-permission="['add']"
                    size="small"
                    type="primary"
                    @click="handleEdit(buttons.add.name)"
                >
                    {{ buttons.add.name }}
                </el-button>
            </template>
            <el-table-column prop="id" label="仓库ID"></el-table-column>
            <el-table-column
                prop="url"
                label="仓库url"
                width="120"
            ></el-table-column>
            <el-table-column prop="username" label="用户名"> </el-table-column>
            <el-table-column prop="password" label="密码">
                <template v-slot="{ row }">
                    <el-tooltip
                        class="item"
                        effect="dark"
                        :content="row.password"
                        placement="top"
                    >
                        <span>
                            {{
                                row.password &&
                                row.password
                                    .split("")
                                    .fill("*", 1, -1)
                                    .join()
                                    .replace(/\,/g, "")
                            }}
                        </span>
                    </el-tooltip>
                </template>
            </el-table-column>
            <el-table-column prop="isActive" label="是否连接成功">
                <template v-slot="{ row }">
                    <el-switch
                        :loading="row.activeLoad"
                        v-model="row.isActive"
                        :active-value="true"
                        :inactive-value="false"
                        active-color="#13ce66"
                        inactive-color="#ff4949"
                        @change="(val) => handelTestRepository(val, row)"
                    >
                        >
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column prop="isDefault" label="是否是默认仓库">
                <template v-slot="{ row }">
                    <el-switch
                        :loading="row.defaultLoad"
                        v-model="row.isDefault"
                        :active-value="true"
                        :inactive-value="false"
                        active-color="#13ce66"
                        inactive-color="#ff4949"
                        @change="(val) => handelSetDefaultRepository(val, row)"
                    >
                        >
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column prop="createTime" label="创建时间">
            </el-table-column>
            <el-table-column prop="updateTime" label="更新时间">
            </el-table-column>
            <el-table-column fixed="right" label="操作">
                <template v-slot:default="{ row }">
                    <el-button
                        v-permission="['edit']"
                        @click.prevent="handleEdit(buttons.edit.name, row)"
                        type="primary"
                        size="small"
                    >
                        {{ buttons.edit.name }}
                    </el-button>
                    <el-button
                        v-permission="['del']"
                        @click.prevent="handleDel(row.id)"
                        type="danger"
                        size="small"
                    >
                        {{ buttons.del.name }}
                    </el-button>
                    <el-button
                        v-permission="['images']"
                        @click.prevent="handleImages(row)"
                        type="info"
                        size="small"
                    >
                        {{ buttons.images.name }}
                    </el-button>
                </template>
            </el-table-column>
        </ve-table>

        <!-- 编辑组件 -->
        <BuilderDockerRepositoryEdit
            v-if="showDialog"
            :rowData="rowData"
            :title="dialogTitle"
            :showDialog="showDialog"
            @closeDialog="handelDialog($event)"
        />
        <DockerRepositoryImagesInfo
            v-if="showImagesDialog"
            :rowData="rowData"
            :showDialog="showImagesDialog"
            @closeDialog="handelCloseImagesDialog($event)"
        />
    </div>
</template>
<script>
import menu from "@/views/pages/builder/components/menu";
import { moduleName } from "@/config";

export default {
    data: () => ({
        description: "仓库管理",
        buttons: {
            search: { name: "查询" },
            add: { name: "添加" },
            edit: { name: "编辑" },
            del: { name: "删除" },
            images: { name: "查看镜像" }, //topath:true 需要设置跳转路径
        },
        // type 0:目录 1：菜单 2：按钮
        type: "1",
        icon: "Stamp",
        name: "仓库管理",
        parentMenu: menu,
        module: moduleName,
    }),
};
</script>

<script setup>
import BuilderDockerRepositoryEdit from "./components/BuilderDockerRepositoryEdit.vue";
import { reactive, toRefs, ref, onMounted, getCurrentInstance } from "vue";

//*导入公共查询方法
import {
    onSubmit,
    resetForm,
    handleSizeChange,
    handleCurrentChange,
} from "@/views/pages/common";
import DockerRepositoryImagesInfo from "@/views/pages/builder/components/DockerRepositoryImagesInfo.vue";

const { proxy } = getCurrentInstance();

const rowData = ref(null);
const dialogTitle = ref("");
const showDialog = ref(false);
const showImagesDialog = ref(false);

const queryForm = ref(null);
const tableData = ref([]);
const params = reactive({
    name: "",
    size: 10,
    current: 1,
    total: 0,
});
const { name, size, current, total } = toRefs(params);

/**
 * @description:添加or编辑事件
 * @return {*}
 * @param title
 * @param row
 */
const handleEdit = (title, row = null) => {
    showDialog.value = true;
    dialogTitle.value = title;
    rowData.value = row;
};
/**
 * @description: dialog事件
 * @return {*}
 * @param e
 */
const handelDialog = (e) => {
    showDialog.value = e;
    getDataList();
};

/**
 * @description: dialog事件
 * @return {*}
 * @param e
 */
const handelCloseImagesDialog = (e) => {
    showImagesDialog.value = e;
    getDataList();
};
/**
 * @description:测试仓库状态
 * @param {*}
 * @return {*}
 */
const handelTestRepository = async (val, row) => {
    if (row.id === undefined) return;
    row.activeLoad = 1;
    const { code } = await VE_API.builder.dockerRepositoryStory(row);
    setTimeout(() => {
        row.activeLoad = 0;
        if (code !== 0) {
            row.isActive = val !== true;
        }
    }, 500);
};
/**
 * @description:设置默认仓库
 * @param {*}
 * @return {*}
 */
const handelSetDefaultRepository = async (val, row) => {
    if (row.id === undefined) return;
    row.defaultLoad = 1;
    const { code } = await VE_API.builder.dockerRepositoryStory(row);
    setTimeout(() => {
        row.defaultLoad = 0;
        if (code !== 0) {
            row.isDefault = val !== true;
        }
    }, 500);
};
/**删除行数据
 * @description:
 * @param {*}
 * @return {*}
 */
const handleDel = (id) => {
    proxy
        .$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "error",
        })
        .then(async () => {
            const { code } = await VE_API.builder.dockerRepositoryRemove({
                id,
            });
            if (code === 0) {
                getDataList();
            }
        })
        .catch(() => {
            proxy.$message({
                type: "info",
                message: "已取消删除",
            });
        });
};
/**
 * @description:查看成员
 * @param {*}
 * @return {*}
 */
const handleImages = (row) => {
    showImagesDialog.value = true;
    rowData.value = row;
};
/**
 * @description: 获取列表数据
 * @param {*}
 * @return {*}
 */
const getDataList = async () => {
    const { code, data } =
        await VE_API.builder.dockerRepositoryFindPage(params);
    if (code === 0) {
        const { size, current, total, records } = data;
        params.size = size;
        params.current = current;
        params.total = total;
        tableData.value = records;
    }
};
onMounted(async () => {
    await getDataList();
    // maxHeight(pagination, queryForm, toolBar, ve_max_height);
});
</script>

<style lang="scss" scoped></style>
